import React, { Component } from 'react';
import Husband from './components/Husband';
import Wife from './components/Wife';

export default class App extends Component {
  // 1. 提升状态state到父组件中
  state = {
    money: 0,
  };

  // 2. 将赚钱的方法提升到父组件中
  /* 赚钱方法 */
  handleMakeMoney = () => {
    this.setState({ money: this.state.money + 1000 });
  };

  // 6. 定义花钱的函数, 并且传给子组件Wife
  handleCost = (num) => {
    console.log('老婆想花钱  ----->  ');
    this.setState({ money: this.state.money - num });
  };

  render() {
    const { money } = this.state;
    return (
      <div>
        <h1 style={{ textAlign: 'center' }}>家庭存款：</h1>
        <Husband
          // 4. 父传子回调函数
          handleMakeMoney={this.handleMakeMoney}
          money={money}
        ></Husband>
        <hr />
        <Wife handleCost={this.handleCost}></Wife>
      </div>
    );
  }
}
